<template>
  <div style="padding: 10px 20px">
    <h2 style="text-align: left; width: 100%">数据查询</h2>
    <el-tabs v-model="typeId" @tab-click="handleClick">
      <template v-for="item in tagArr">
        <el-tab-pane :key="item.id" :label="item.name" :name="item.id" lazy>
          <info-search
            ref="infoSearch"
            :pagination="pagination"
            @submit="getInfoList"
          ></info-search>
        </el-tab-pane>
      </template>
    </el-tabs>
    <div>
      <div style="display: flex; align-items: center">
        <h3>资讯列表</h3>
        <el-button style="margin-left: 20px" type="primary" @click="addInfo">
          添加资讯
        </el-button>
      </div>
      <div v-for="item in datas" :key="item.id">
        <div class="itemBox">
          <div class="imgBox">
            <img :src="item.photoUrl"/>
          </div>
          <div class="itemInfoBox">
            <div class="item">
              <h3>{{ item.title }}</h3>
              <div style="padding: 5px 0px">阅读:{{ item.readCount }} 评论：{{ item.commentCount }}</div>
              <div style="padding: 5px 0px">
                <el-tag v-if="item.top == 1">置顶</el-tag>
                <el-tag>{{ showState(item.stateId) }}</el-tag>
              </div>
              <div style="padding: 5px 0px">
                <span>{{ item.maName }}</span>
                <span>{{ item.createdTime }}</span>
              </div>
            </div>
            <div class="option">
              <span class="optionItem" @click="() => setInfoTop(item)"> {{item.top==1?'取消置顶':'置顶'}}</span>
              <span class="optionItem" @click="() => updateInfo(item)">编辑</span>
              <span class="optionItem" @click="() => updateInfo(item)">下架</span>
              <span class="optionItem" style="color: red" @click="() => deleteInfo(item.id)">删除</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <el-pagination
        :current-page="pagination.pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import InfoSearch from './component/infoSearch.vue'
import {getList, setTop, getColumn,upDate,deleteInfo,addInfo} from "@/api/information"

export default {
  name: 'Information',
  components: {
    'info-search': InfoSearch,
  },
  data() {
    return {
      datas: [],
      typeId: '1',
      tagArr: [],
      pagination: {
        pageSize: 10,
        pageNum: 1,
      },
      total: 0,
      tagIndex: 0
    }
  },
  computed: {},
  async created() {
    const res = await getColumn()
    if (res && res.code === 200) {
      res.data.map(item => {
        return item.id = String(item.id)
      })
      this.tagArr = res.data
      this.typeId = res.data[0].id
    }
  },
  methods: {
    async getInfoList(value) {
      const params = {
        ...this.pagination,
        ...value,
        typeId: this.typeId,
      }
      const res = await getList(params)
      if (res.code === 200) {
        this.datas = res.data.list
        this.pagination.pageSize = res.data.pageSize
        this.pagination.pageNum = res.data.pageNum
        this.total = res.data.total
      }
    },
    async setInfoTop(data) {
      const res = await setTop({...data})
      if (res && res.code == 200) {
        this.getInfoList(this.$refs.infoSearch[this.tagIndex].searchForm)
      }
    },

    async updateInfo(data) {
      const res = await upDate(data)
      if (res && res.code == 200) {
        this.getInfoList(this.$refs.infoSearch[this.tagIndex].searchForm)
      }
    },

    async deleteInfo(data) {
      const res = await deleteInfo(data)
      if (res && res.code == 200) {
        this.getInfoList(this.$refs.infoSearch[this.tagIndex].searchForm)
      }
    },

    showState(type) {
      if (type) {
        switch (type) {
          case 1:
            return '已发布'
          case 2:
            return '审核中'
          case 3:
            return '未通过'
          case 4:
            return '草稿'
          case 5:
            return '已下架'
        }
      } else {
        return ''
      }
    },
    handleClick(value) {
      this.tagIndex = value.index
      this.$nextTick(() => {
        this.$refs.infoSearch[value.index].resetForm('searchForm')
      })
    },
    addInfo() {
      this.$router.push({
        path: `/contentManagement/infoForm`
      })
    },
    handleSizeChange(value) {
      console.log(this.$refs.infoSearch)
      this.pagination.pageSize = value
      this.getInfoList(this.$refs.infoSearch[this.tagIndex].searchForm)
    },
    handleCurrentChange(value) {
      this.pagination.pageNum = value
      this.getInfoList(this.$refs.infoSearch[this.tagIndex].searchForm)
    },
  },
}
</script>

<style scoped>
.itemBox {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.itemInfoBox {
  display: flex;
  align-items: end;
  justify-content: space-between;
  width: 80%;
}

.imgBox {
  width: 160px;
  height: 120px;
}

.option {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.optionItem {
  cursor: pointer;
  padding: 0px 10px;
}

</style>
